<?php
date_default_timezone_set("Europe/London");
define('__ROOT__', __DIR__);
define('DS', DIRECTORY_SEPARATOR);

/*
 * Load libraries and required files
 */
if(!include(__ROOT__.DS."sock".DS."SocketServerBroadcast.php")){
    die("Unable to load socketserver libraries.");
}
if(!include(__ROOT__.DS.'sock'.DS.'SocketHandler.php')){
    die("Unable to load socketserver base handler");
}

//MUDServer loading
\Sock\SocketServer::cLog("Loading MUD files");
$mudfiles=array(
    __ROOT__.DS.'library'.DS.'R.class.php',
    __ROOT__.DS.'phpreg.class.php',
    __ROOT__.DS.'library'.DS.'dbinterface.class.php',
    __ROOT__.DS.'library'.DS.'strings.class.php',
    __ROOT__.DS.'library'.DS.'R.class.php',
    __ROOT__.DS.'library'.DS.'client.class.php',
    __ROOT__.DS.'MUDHandler.php',
    __ROOT__.DS.'library'.DS.'PasswordHash.php',
    __ROOT__.DS.'library'.DS.'map.class.php',
    __ROOT__.DS.'library'.DS.'room.class.php'
);
for($i=0,$mfc=count($mudfiles);$i<$mfc;$i++){
    if(!include_once($mudfiles[$i])){
        die("\t[ ERROR ]\nUnable to load library file: ".$mudfiles[$i]."\n\n".$php_errormsg."\n\n");
    }
}
echo "\t[ OK ]\n";

\Sock\SocketServer::cLog("Loading rooms");
$mudrooms = array(
    __ROOT__.DS.'rooms'.DS.'reception.class.php'
);
for($i=0,$mrc=count($mudrooms);$i<$mrc;$i++){
    if(!include_once($mudrooms[$i])){
        die("\t[ ERROR ]\nUnable to load room: ".$mudrooms[$i]."\n\n".$php_errormsg."\n\n");
    }
}
echo "\t[ OK ]\n";

\Sock\SocketServer::cLog("Opening connection to database server");
$dbo = new MUD\DBInterface(MUD\PHPREG::$db_host,MUD\PHPREG::$db_user, MUD\PHPREG::$db_pass,MUD\PHPREG::$db_name,MUD\PHPREG::$db_prefix);
$dbo->connect();
echo "\t[ OK ]\n";
MUD\R::current()->setDBInterface($dbo);

/*\Sock\SocketServer::cLog("Loading items");
$items = new \MUD\Items;
echo "\t[ OK ]\n";
MUD\R::current()->setItemsRes($items);*/

function onConnect($client){
    
    $pid = pcntl_fork();
    if($pid == -1){
        \Sock\SocketServer::cLog("Unable to create process for new client.");
        break;
    }else if($pid){
        return $pid;
    }
    
    \Sock\SocketServer::cLog("New client connected from ".$client->getAddress());
    
    \MUD\R::current()->setClient($client);
    $handler = &new \MUD\Handler;
    
    \MUD\R::current()->client->send("Connected on ".date('jS F Y', time())." at ".date('H:i', time())." from ".\MUD\R::current()->client->getAddress()."\n\n");
    
    if(method_exists($handler, 'onConnect')){
        $handler->onConnect();
    }
    
    $read='';
    while(true){
        $read=\MUD\R::current()->read();
        if($read === ''){
            //client has disconnected
            if(method_exists($handler, 'onDisconnect')){
                $handler->onDisconnect();
            }
            break;
        }
        $data = trim($read);
        if($data == 'quit'){
            //client is requesting to quit
            if(method_exists($handler, 'onQuit')){
                $handler->onQuit();
            }
            \MUD\R::current()->client->disconnected();
            \Sock\SocketServer::cLog("Client ".$client->getAddress()." has quit.");
        }else{
            $handler->onReceiveData($data);
        }
    }
}

\Sock\SocketServer::cLog("Creating connection point for incomming clients");

$ipaddress = $argv[1];

$server = new \Sock\SocketServerBroadcast(9000, $ipaddress);
$server->init();
$server->setConnectionHandler('onConnect');
$server->listen();